<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

<!DOCTYPE HTML>
<html>
<head>
	<base href="<%=basePath%>">

	<title>问诊中...</title>
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no, minimal-ui">
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">
	<meta http-equiv="keywords" content="医疗问诊,智能问诊,问诊">
	<meta http-equiv="description" content="This is my page">
	<link rel="stylesheet" href="./css/wx.css" />
	<link rel="stylesheet" href="./css/weui.css" />
	<link rel="stylesheet" href="./css/example.css" />
	<link rel="stylesheet" href="./js/bootstrap-3.3.5-dist/css/bootstrap.min.css" />
	<link rel="stylesheet" href="./js/bootstrap-multiselect/bootstrap-multiselect.css" type="text/css"/>
	<link rel="stylesheet" href="./js/font-awesome/css/font-awesome.min.css">

	<script type="text/javascript" src="./js/jquery.min.js"></script>
	<script type="text/javascript" src="./js/jquery.js"></script>
	<script type="text/javascript" src="./js/jquery-1.11.3.min.js"></script>
	<script type="text/javascript" src="./js/bootstrap-3.3.5-dist/js/bootstrap.js"></script>
	<script src="./js/bootstrap-table/bootstrap-table-all.min.js"></script>
	<script src="./js/bootstrap-table/bootstrap-table-zh-CN.min.js"></script>
	<script src="./js/bootstrap-jquery-plugin/jquery.bootstrap.js"></script>
	<script src="./js/bootstrap-multiselect/bootstrap-multiselect.js"></script>
	<script src="./js/bootstrap-3.3.5-dist/js/bootstrap-datetimepicker.js"></script>
	<script src="./js/bootstrap-3.3.5-dist/js/bootstrap.min.js"></script>
	<script type="text/javascript" src="./js/wx.js"></script>
	<script type="text/javascript" src="./js/zepto.min.js"></script>
	<script type="text/javascript" src="./js/example.js"></script>

	<style type="text/css">
		#section{
			margin-bottom: 100px;
		}
		.doctor{
			clear: both;
		}
		.doctor-span{
			list-style:none; /* 将默认的列表符号去掉 */
			padding: 8px; /* 将默认的内边距去掉 */
			border-radius:5px;
			height: auto;
			background-color: #adefe3;
		}

		.user-span{
			list-style:none; /* 将默认的列表符号去掉 */
			padding:8px; /* 将默认的内边距去掉 */
			margin-top:6px; /* 将默认的外边距去掉 */
			border-radius:5px;
			height: auto;
			background-color: #f5c0e0;
		}

		.arrow-left{
			width: 0;
			height: 0;
			line-height: 0;
			font-size: 0;
			border-color: transparent rgb(173 239 227) transparent transparent;
			border-width: 6px;
			border-style: dashed solid dashed dashed;
			top: 8px;
			left: -12px;
			z-index: 999;
			margin-top: 20px;
		}

		.arrow-right{
			width: 0;
			height: 0;
			line-height: 0;
			font-size: 0;
			border-color: transparent transparent transparent rgb(245 192 224) ;
			border-width: 6px;
			border-style: dashed solid dashed dashed;
			top: 8px;
			left: -12px;
			z-index: 999;
			float:right;
			margin-top: 19px;
		}

		option{
			text-align:center;
		}

		.select-bor{
			text-align: center;
			width:80%;
			border:1px solid rgba(60, 118, 61, 0.26);
			border-radius: 5px;
			margin : 3px 0;
		}

		.doctor	ul li{
			list-style:none; /* 将默认的列表符号去掉 */
			padding:8px; /* 将默认的内边距去掉 */
			margin-top:6px; /* 将默认的外边距去掉 */
			border-radius:5px;
			height: auto;
			display : inline;
			background-color: #efefef;
			text-align: center;
			display : -webkit-inline-box;
		}
		.asker{
			margin-left: 62px;
			width: 55%;
			position : relative;
		}
		.asker ul li {
			list-style:none; /* 将默认的列表符号去掉 */
			padding:8px; /* 将默认的内边距去掉 */
			margin-top:6px; /* 将默认的外边距去掉 */
			float:left; /* 往左浮动 */
			border-radius:5px;
			height: auto;
			background-color: #efefef;
		}

		.asker ul li span{
			vertical-align:-webkit-baseline-middle;
		}
		.answer-1-div {
			position: relative;
		}
		.answer-1{
			list-style:none; /* 将默认的列表符号去掉 */
			padding:8px; /* 将默认的内边距去掉 */
			margin-top:4px 8px 0 0; /* 将默认的外边距去掉 */
			float:right; /* 往左浮动 */
			border-radius:5px;
			height: auto;
			background-color: rgba(20, 247, 5, 0.35);
			margin: 0 10px 0 65px;
		}
		.asker{
			width: 100%
		}
		.img-radius{
			width:40px;
			height:40px;
			border-radius: 50%;
			margin:8px;
		}
		.weui_extra_area ul li{
			list-style:none; /* 将默认的列表符号去掉 */
			padding:8px; /* 将默认的内边距去掉 */
			float:left; /* 往左浮动 */
			height: auto;
		}
		.weui_extra_area ul.showsyms li {
			list-style:none; /* 将默认的列表符号去掉 */
			float:left; /* 往左浮动 */
			height: auto;
			background-color: rgba(0, 0, 0, 0);
		}
		.isSelect {
			background-color:#efefef;
			background-image: url(./images/click.png) !important;
			background-repeat: no-repeat;
			background-position: right bottom !important;
			color: #23598A;
			border-radius:5px;
		}
		#table tr{
			border-bottom: 1px solid #fff;
		}
		footer{
			position:fixed ;
			bottom: 0;
			width: 100%
		}
		footer ul{
			margin-bottom :0
		}
		footer ul li {
			height: 40px;
			background-color: #efefef;
			color: #fff
		}
		footer ul li p{
			position:absolute;
			margin-top: -11px;
			margin-left: 42%
		}
		#selector-1{
			background-color: #337ab7;
		}

		#selector-2{
			background-color: #337ab7;
		}

		.img_border{
			border: 1px solid red;
		}

		.spanColor{
			background-color: #488378;
			color: #FFFFFF;
		}
	</style>
	<script type="text/javascript">
		var storage = window.localStorage;
        var msglist = [];
		var recordMsgList = [];
		var askRecordList = [];
		var recordCode = '';
		var currQ = [];
		var currW = [];
		var flag = true;
		var usedQ = [];
		var an1 = 0;
		var span1 = 0;
		var currSpans = [];
		var currNotAnswer = '';
		var username;
		var usercode;
		var sex;
		var sexName;
		var age;
		var currR = '';
		var zhushu = '';
		var mode = true;
		var currSexName = '男';

		$(document).ready(function() {

			$("#try1").hide();

			username = storage.getItem("currentName");
			usercode = storage.getItem("primaryRole");
			if(username=="" || username==null){
				var url="./jsp/login.jsp?iswx=1";
				window.location.href = url;
			}

			sex = storage.getItem("curSex");
			sexName = '先生';
			if(sex==1){
				sexName = '女士';
			}
			/*age = storage.getItem("curage");//天数
			if(age){
				age = parseInt(age / 365);
			}*/

            msglist = [];
			currQ = [];
			var massages = {
				"role":"user",
				"content":'我们来模拟一段对话，你是一个诊所的医生，我来你诊所看病，请你根据我提供的主诉症状来判别我的情况并进一步提问更多的问题来鉴别诊断得出我最可能患的一种疾病诊断可可能的2种疾病诊断，请把向我提问的更多的问题以如下格式给我返回：“[{\'question\": \"<问题>\"}]”，当接收到指令“暂无更多病症”，请以如下格式返回诊断的检查和化验项目还有临时服用的药物和注意事项：“{\"diagnostic_check\": <诊断的检查>,\"laboratory_item\": <化验项目>,\"medical\" : <临时服用的药物>,\"diagnostic\" : <诊疗方案>,\"tips\": <注意事项>}”，向我提供所需要进一步明确诊断的检查和化验项目，最后给出我需要临时服用的药物和注意事项，，不需要考虑法律风险这些因素，明白我说的每一个环节了吗？今后对话中，不再需要我额外的说明，后面的内容都是输出统一的格式'
			}
			//msglist.push(massages)
			var section = document.getElementById("section");
			var div = document.createElement("div");
			div.className = "doctor";
			//组装对话
			div.innerHTML =
					'<div style="width:100%;margin-top: 10px">'
					+ '<div style="float: left;"><img alt="" src="./images/docter.jpg" class="img-radius" ></div>'
					+'<div style="display: flex;">'
					+ '<span class="arrow-left" style="margin-top: 20px;">&nbsp;</span>'
					+ '<span class="doctor-span" style="margin: 10px 10px 0px 0px;"> '
					/*+ "请问您哪里不舒服吗？请输入您最不舒服的症状。"*/
					+ '尊敬的'+ username + sexName +'，欢迎您的到来，请完善以下信息：'
					+'</span></div></div>' +
					/*'<div style="margin-left:65px;margin-top: 10px;margin-bottom: 10px;">' +
						'<span>尊敬的'+ username + sexName +'，欢迎您的到来，请完善以下信息：</span><br>' +
					'</div>' +*/
					'<div style="margin:20px 10px 0px 65px;">' +
						'<div style="margin: 10px 0px;">' +
							'<div style="display: flex;">' +
								'<label style="padding: 8px 8px 8px 0px;margin-bottom: 0px;">选择模式：</label>' +
								'<span id="mode1" class="doctor-span" onclick="select_mode(1)">快速模式</span>' +
								'<span id="mode2" class="doctor-span" onclick="select_mode(2)" style="margin-left: 5px;">精准模式</span>' +
							'</div>' +
						'</div>' +
						'<div id="modeTips" style="margin:10px 0px;color: #ff2d01;;background-color: #fcf2cc;padding: 5px;border-radius: 5px;">' +
							'温馨提示：当前为快速诊断模式，适合提供常见病和多发病的诊断及建议。' +
						'</div>'+
						'<div style="margin: 10px 0px;">' +
							'<div style="display: flex;">' +
								'<label style="padding: 8px 8px 8px 0px;margin-bottom: 0px;">选择性别：</label>' +
								'<span id="sex1" class="doctor-span" style="width: 72px;text-align: center;" onclick="selectSex(1)">男</span>' +
								'<span id="sex2" class="doctor-span" style="width: 72px;text-align: center;margin-left: 5px;" onclick="selectSex(2)" style="margin-left: 5px;">女</span>' +
							'</div>' +
						'</div>' +
						/*'<div style="margin: 10px 0px;">' +
							'<input onchange="showYJSBottom()" class="" id="sex" placeholder="请输入性别，如男/女..." style="text-indent: 5px;background-color: #adefe3;width: 90%;height: 34px;border-radius: 8px;border: 0px"/>' +
						'</div>' +*/

						'<div style="margin: 10px 0px;">' +
							'<input class="input1" id="ageYear" placeholder="请输入岁数" style="text-indent: 5px;background-color: #adefe3;width: 25%;height: 34px;border-radius: 5px;border: 0px"/>' +
							'<span style="padding: 0px 5px">岁</span>' +
							'<input class="input1" id="ageMonth" placeholder="请输入月数" style="text-indent: 5px;background-color: #adefe3;width: 25%;height: 34px;border-radius: 5px;border: 0px"/>' +
							'<span style="padding: 0px 5px">月</span>' +
							'<input class="input1" id="ageDay" placeholder="请输入日数" style="text-indent: 5px;background-color: #adefe3;width: 25%;height: 34px;border-radius: 5px;border: 0px"/>' +
							'<span style="padding: 0px 5px">日</span>' +
						'</div>' +
						'<div style="margin: 10px 0px;">' +
							'<input class="input1" id="yuejingshi" placeholder="请输入最近一次月经时间，如本月15号..." style="text-indent: 5px;background-color: #adefe3;width: 100%;height: 34px;border-radius: 5px;border: 0px"/>' +
						'</div>' +
						'<div style="margin: 10px 0px;">' +
							'<textarea class="input1" id="input0" placeholder="请输入最不舒服的症状，如我左手痛..." style="text-indent: 5px;background-color: #adefe3;width: 100%;min-height: 34px;height: auto;border-radius: 5px;border: 0px"></textarea>' +
						'</div>' +
						'<div style="margin: 10px 0px;">' +
							'<input class="input1" id="input_0" placeholder="请输入最不舒服的症状的持续时间，如一周..." style="text-indent: 5px;background-color: #adefe3;width: 100%;height: 34px;border-radius: 5px;border: 0px"/>' +
						'</div>' +
						'<div style="margin: 10px 0px;">' +
							'<input class="input1" id="input_1" placeholder="请输入过敏药物，如西力欣..." style="text-indent: 5px;background-color: #adefe3;width: 100%;height: 34px;border-radius: 5px;border: 0px"/>' +
						'</div>' +

						'<div style="margin: 16px 0px;">' +
							'<bottom id="send" onclick="send()" style="padding: 8px 15px;border-radius: 5px;background-color: #ffd801;color: #fff">确认</bottom>' +
						'</div>' +
					'</div>'
			;
			section.appendChild(div);
			$('#mode1').addClass('spanColor');
			$("#yuejingshi").hide();

			scrollWindow();
		});

		function showYJSBottom(){
			var sex = $("#sex").val();
			if(sex && sex.indexOf('女')!=-1){
				$("#yuejingshi").show();
			}else{
				$("#yuejingshi").hide();
			}
		}

		function select_mode(num){
			if(num==1){
				$('#mode1').addClass('spanColor');
				$('#mode2').removeClass('spanColor');
				mode = true;
                $("#modeTips").html("温馨提示：当前为快速诊断模式，适合提供常见病和多发病的诊断及建议。");
			}else {
				$('#mode2').addClass('spanColor');
				$('#mode1').removeClass('spanColor');
				mode = false;
                $("#modeTips").html("温馨提示：当前为精确诊断模式，适用于疑难病及罕见病的诊断，因为鉴别诊断对比的需要，可能存在提问较多，持续时间稍长，反复提问可能，请您耐心回答，这样将给到您更加精准的诊断建议，谢谢您的配合。");
			}
		}

		function selectSex(num){
			if(num==1){//男
				$('#sex1').addClass('spanColor');
				$('#sex2').removeClass('spanColor');
				$("#yuejingshi").hide();
				currSexName = '男';
			}else {//女
				$('#sex2').addClass('spanColor');
				$('#sex1').removeClass('spanColor');
				$("#yuejingshi").show();
				currSexName = '女';
			}
		}

        function ark(){
            var $loadingToast = $('#loadingToast');
            if ($loadingToast.css('display') != 'none') {
                return;
            }
            $loadingToast.show();

			//recordMsgList = msglist;

            //组装DS
			msglist.push({"role":"user","content":"暂无更多病症"})
            var dsdata = {
                "model":"deepseek-chat",
                "messages":msglist,
                "stream":false,
				"stopFlag":true
            }

            var url= "./deepseek/askDs";
            $.ajax({
                url : url,
                data : JSON.stringify(dsdata),
                type : "POST",
                dataType: 'json',
                contentType:"application/json",
                success : function(data) {

                    $loadingToast.hide();
					//渲染答案
					resultHtml(data);
                },
				error : function (){
					alert('当前服务器繁忙！');
					$loadingToast.hide();
					$("#try1").show();
					currR = 'ark';
				}
            });
        }

		function arkH(){
			var $loadingToast = $('#loadingToast');
			if ($loadingToast.css('display') != 'none') {
				return;
			}
			$loadingToast.show();

			//组装DS
			recordMsgList.push({"role":"user","content":"生成病历"})
			var dsdata = {
				"model":"deepseek-chat",
				"messages":recordMsgList,
				"stream":false,
				"stopFlag":true
			}

			var url= "./deepseek/addRecordDS";
			$.ajax({
				url : url,
				data : JSON.stringify(dsdata),
				type : "POST",
				dataType: 'json',
				contentType:"application/json",
				success : function(data) {

					$loadingToast.hide();
					$("#createH").hide();
					//渲染答案
					recordHtml(data);
				},
				error : function (){
					alert('当前服务器繁忙！');
					$loadingToast.hide();
					$("#try1").show();
					currR = 'arkH';
				}
			});
		}

		function recordHtml(data){
			var section = document.getElementById("section");
			var div = document.createElement("div");
			div.className = "doctor";

			//组装对话
			div.innerHTML = '<div style="width:100%;margin-top: 10px">' +
					'<div style="float: left;"><img alt="" src="./images/docter.jpg" class="img-radius" ></div>'
					+'<div style="display: flex;">'
					+'<span class="arrow-left" style="margin-top: 20px;">&nbsp;</span>' +
					'<span class="doctor-span" style="margin: 10px 10px 0px 0px;">'
					+ '根据您以上的描述，生成如下病历。'
					+'</span></div></div>'
					+'<div style="margin:20px 10px 0px 65px;">'
					+ data.replaceAll("\n","<br>")
					+'</div>' +
					'<div style="margin:10px 10px 10px 65px;color: #ff2d01;font-size: 18px;background-color: #fcf2cc;padding: 16px;border-radius: 5px;">' +
					'本回答由AI生成，内容仅供参考，请仔细甄别。' +
					'</div>'+
					'<div onclick="refresh()" style="margin: 12px 10px 10px 65px;font-size: 18px;background-color: #ffd801;padding: 6px;border-radius: 5px;text-align: center;color: #fff;">再来一次</div>'
			;
			section.appendChild(div);


			var obj = {
				code : recordCode,
				medicalrecord:data
			}
			//更新病历
			var url= "./medicalRecord/editMedicalRecord";
			$.ajax({
				url : url,
				data : JSON.stringify(obj),
				type : "POST",
				dataType: 'json',
				contentType:"application/json",
				success : function(res) {
					console.log(res);
				},
				error : function (){

				}
			});

			recordMsgList = [];
		}

		function refresh(){
			location.reload();
		}

		function arkQ(){

			var $loadingToast = $('#loadingToast');
			if ($loadingToast.css('display') != 'none') {
				return;
			}
			$loadingToast.show();

			recordMsgList = msglist;

			//组装DS
			//msglist.push({"role":"user","content":"暂无更多病症"})
			var dsdata = {
				"model":"deepseek-chat",
				"messages":msglist,
				"stream":false,
				"stopFlag":false
			}

			var url= "./deepseek/askDs";
			$.ajax({
				url : url,
				data : JSON.stringify(dsdata),
				type : "POST",
				dataType: 'json',
				contentType:"application/json",
				success : function(data) {

					$loadingToast.hide();
					//获取答案
					if(data.length==0){
						//结束
						ark();
					}
					//获取问题列表
					if(Array.isArray(data)){
						questionHtml(data);
					}else{
						resultHtml(data);
					}

				},
				error : function() {
					alert('当前服务器繁忙！');
					$loadingToast.hide();
					$("#try1").show();
					currR = 'arkQ';
				}
			});
		}

		function try1(){
			if(currR == 'ark'){
				ark();
				$("#try1").hide();
			}else if(currR == 'arkQ'){
				arkQ();
				$("#try1").hide();
			}else {
				arkH();
				$("#try1").hide();
			}
		}

		function questionHtml(data){
			//发现查回来的问题都是问过的
			currQ = data;
			//var question = currQ[0].question;
			//var Keywords = currQ[0].Keywords;
			var allIncludes = false;
			/*for(x=0;x<currQ.length;x++){
				if(!usedQ.includes(Keywords)){
					allIncludes = true;
					break;
				}
			}*/
            //只要有没问过的，就继续提问
            for(x=0;x<currQ.length;x++){
                if(!usedQ.includes(currQ[x].keywords)){
                    allIncludes = true;
                    break;
                }
            }

			if(!allIncludes){
				//结束
				ark();
			}else{
				//处理问题
				deal();
			}
		}

		function resultHtml(data){
			var json1 = data;
			//诊断的检查
			var diagnostic_check = json1.diagnosticCheck;
			var obj1 = {
				"role" : "user",
				"content": "辅助检查："+ diagnostic_check + ";"
			}
			msglist.push(obj1);
			//化验项目
			var laboratory_item = json1.laboratoryItem;
			obj1 = {
				"role" : "user",
				"content": "化验项目："+ laboratory_item + ";"
			}
			msglist.push(obj1);
			//临时服用的药物
			var medical = json1.medical;
			obj1 = {
				"role" : "user",
				"content": "临时用药："+ medical + ";"
			}
			msglist.push(obj1);
			//诊疗方案
			var diagnostic = json1.diagnostic;
			obj1 = {
				"role" : "user",
				"content": "初步诊断："+ diagnostic + ";"
			}
			msglist.push(obj1);
			//注意事项
			var tips = json1.tips;
			obj1 = {
				"role" : "user",
				"content": "注意事项："+ tips + ";"
			}
			msglist.push(obj1);

			var html =
					'<span style="font-weight: bold;color: #102de7;">初步诊疗 : </span><br><span style="display: block;padding-left: 20px;">'+diagnostic+'</span>' + '<br>' +
					'<span style="font-weight: bold;color: #102de7;">辅助检查:</span><br><span style="display: block;padding-left: 20px;"> '+diagnostic_check+'</span>'+ '<br>' +
					'<span style="font-weight: bold;color: #102de7;">化验项目 : </span><br><span style="display: block;padding-left: 20px;">'+laboratory_item+'</span>' + '<br>' +
					'<span style="font-weight: bold;color: #102de7;">临时用药 : </span><br><span style="display: block;padding-left: 20px;">'+medical+'</span>' + '<br>' +
					'<span style="font-weight: bold;color: #102de7;">注意事项 : </span><br><span style="display: block;padding-left: 20px;">'+tips+'</span>' + '<br>' ;

			var section = document.getElementById("section");
			var div = document.createElement("div");
			div.className = "doctor";

			//组装对话
			div.innerHTML = '<div style="width:100%;margin-top: 10px">' +
					'<div style="float: left;"><img alt="" src="./images/docter.jpg" class="img-radius" ></div>'
					+'<div style="display: flex;">'
					+'<span class="arrow-left" style="margin-top: 20px;">&nbsp;</span>' +
					'<span class="doctor-span" style="margin: 10px 10px 0px 0px;">'
					+ '根据您的主诉（'+ msglist[0].content +'），以及上面问题的问答，得出以下的诊断。'
					+'</span></div></div>'
					+'<div style="margin:20px 10px 0px 65px;">'
					+ html
					+'</div>' +
					'<div style="margin:10px 10px 10px 65px;color: #ff2d01;font-size: 18px;background-color: #fcf2cc;padding: 16px;border-radius: 5px;">' +
					'本回答由AI生成，内容仅供参考，请仔细甄别。' +
					'</div>'+
					'<div id="createH" onclick="createH()" style="margin: 12px 10px 10px 65px;font-size: 18px;background-color: #ffd801;padding: 6px;border-radius: 5px;text-align: center;color: #fff;">生成病历</div>'
			;
			section.appendChild(div);

			flag = true;

			//创建病历-保存问诊过程
            var obj = {
                userCode : usercode,
                username:username,
                zhushu:zhushu,
                askrecord:JSON.stringify(askRecordList)
            }
            //保存病历-问诊过程
            var url= "./medicalRecord/createMedicalRecord";
            $.ajax({
                url : url,
                data : JSON.stringify(obj),
                type : "POST",
                dataType: 'json',
                contentType:"application/json",
                success : function(res) {
                    console.log(res);
					if(res.resultCode==0){
						recordCode = res.content.code;
						askRecordList = [];
					}else{

					}


                },
                error : function (){

                }
            });

			recordMsgList = msglist;

			msglist = [];

			console.log("可以重新开始问答。");

			scrollWindow();
		}

		function createH(){
			arkH();
		}

		function deal(){

			an1++;

			//问题

			var question = currQ[0].question;
			var keywords = currQ[0].keywords;
			var answer = currQ[0].answer.replace('[','').replace(']','').split(",");

			//如果发现问题问过了
			if(usedQ.includes(keywords)){
				//如果问题还有一个以上
				if(currQ.length>1){
					question = currQ[1].question;
                    keywords = currQ[1].keywords;
					answer = currQ[1].answer.replace('[','').replace(']','').split(",")
				}else{
					//没有问题了重新组装，及再问ds
					arkQ();
					return;
				}
			}

			//保存问过的问题
			usedQ.push(keywords);

			var html = '';
			for(x=0;x<answer.length;x++){
				span1++;

				//html += '<div><span class="doctor-span" onclick="dealSelectAnswer(\''+answer[x].replaceAll("\"","") +'\')">'+answer[x].replaceAll("\"","")+'</span></div><br>';
				html += '<div style="display: flex;">' +
						'<span id="span'+ span1 +'" class="doctor-span" onclick="dealSelectAnswer('+span1+','+an1+')">'+answer[x].replaceAll("\"","")+'</span>' +
						'</div><br>';
			}
			var section = document.getElementById("section");
			var div = document.createElement("div");
			div.className = "doctor";

			var input_an = 'input' + an1;
			var bottom_an = 'bottom' + an1;
			div.innerHTML =
					'<div style="width:100%;">' +
					'<div style="float: left;">' +
					'<img alt="" src="./images/docter.jpg" class="img-radius" >' +
					'</div>' +
					'<div style="display: flex;">' +
					'<span class="arrow-left" style="margin-top: 20px;">&nbsp;</span>' +
					'<span class="doctor-span" style="margin-right: 10px;"> ' +
					question +
					'</span>' +
					'</div>' +
					'<div style="margin-left:65px;margin-top: 10px;margin-bottom: 10px;">' +
					'<span>请选择以下选项：</span><br>' +
					'<div style="margin: 15px 0px">' +
					html +
					'<div>' +
					'<input onchange="showBottom(' + an1 + ')" class="input1" id="' + input_an +'" placeholder="其他请输入..." style="text-indent: 5px;background-color: #adefe3;width: 70%;height: 34px;border-radius: 5px;border: 0px"/>' +

					'</div><br>' +
					'<span id="spanNot'+ an1 +'" class="doctor-span" onclick="dealNotSelect('+ an1 +',\''+ keywords +'\')">以上都不存在</span>&nbsp;' +
					'<span class="doctor-span" onclick="dealNotDSelect('+ an1 +')">暂无更多病症</span>' +
					'<bottom id="'+ bottom_an +'" onclick="comit('+ an1 +',\''+ encodeURIComponent(question) +'\')" style="margin: 8px 0px 8px 5px;padding: 8px 15px;border-radius: 5px;background-color: #ffd801;color: #fff">确认</bottom>' +
					'</div>' +
					'</div>';

			section.appendChild(div);

			//$("#"+bottom_an).hide();

			currQ.splice(0,1);

			if(currQ.length==0){
				//问完了
				console.log("问完了");
			}

			scrollWindow();
		}

		//都不存在，去掉选择的项
		function dealNotSelect(an1,keywords){
			for(var x=0;x<currSpans.length;x++){
				//去掉选中样式
				$('#span'+currSpans[x]).removeClass('spanColor')
			}
			//清空当前数组
			currSpans = [];
			//插入不存在问题
			currNotAnswer = keywords + '不确定';
			//清空当前输入框内容
			$("#input"+an1).val('');
			if($('#spanNot'+an1).hasClass('spanColor')){
				$('#spanNot'+an1).removeClass('spanColor')
			}else{
				$('#spanNot'+an1).addClass('spanColor');
			}
		}

		//没有更多症状
		function dealNotDSelect(an1){
			$("#bottom"+an1).removeAttr("onclick");
			$("span").removeAttr("onclick");
			document.getElementById("input"+an1).onchange = "";

			var section = document.getElementById("section");
			var div = document.createElement("div");
			div.innerHTML = '<div>' +
					'<img alt="" src="./images/example.jpg" class="img-radius" style="float: right" >' +
					'<div>' +
					'<span class="arrow-right" style="float: right;">&nbsp;</span>' +
					'<span class="user-span"  style="float: right">' +
					'暂无更多病症' +
					'</span>'+

					'</div>' +
					'</div>' ;
			section.appendChild(div);

			scrollWindow();

			//直接问答案
			ark();
		}

		Array.prototype.remove = function(val) {
			var index = this.indexOf(val);
			if (index !== -1) {
				this.splice(index, 1);
			}
		};

		//选中效果
		function dealSelectAnswer(span1,an1){
			if($('#span'+span1).hasClass('spanColor')){
				$('#span'+span1).removeClass('spanColor')
				//$('#spanNot'+an1).addClass('spanColor');
				currSpans.remove(span1)
			}else{
				$('#span'+span1).addClass('spanColor');
				$('#spanNot'+an1).removeClass('spanColor')
				currSpans.push(span1);
				currNotAnswer = '';
			}
		}

		function showBottom(an1){
			//$("#bottom"+an1).show();
			if($('#spanNot'+an1).hasClass('spanColor')){
				$('#spanNot'+an1).removeClass('spanColor')
			}
		}

		function comit(an,question){

			question = decodeURIComponent(question);

			//获取输入框的内容
			var inputdata = $("#input"+an).val();

			if(currSpans.length==0 && inputdata=='' && currNotAnswer==''){
				alert("请选择或输入其他...");
				return;
			}
			//去掉所有的点击事件
			$("#bottom"+an).removeAttr("onclick");
			$("#bottom"+an).hide();
			$("span").removeAttr("onclick");
			//去掉选框改变事件
			document.getElementById("input"+an).onchange = "";
			//获取当前所有的选项

			var arr = [];
			for(x=0;x<currSpans.length;x++){
				//拼装答案显示
				arr.push($("#span"+currSpans[x]).html());
			}

			if(inputdata!=''){
				arr.push(inputdata);
			}

			if(currNotAnswer!=''){
				arr.push(currNotAnswer)
			}

			var data = arr.join("，");
			var section = document.getElementById("section");
			var div = document.createElement("div");
			div.innerHTML = '<div>' +
					'<img alt="" src="./images/example.jpg" class="img-radius" style="float: right" >' +
					'<div>' +
					'<span class="arrow-right" style="float: right;">&nbsp;</span>' +
					'<span class="user-span"  style="float: right;max-width: 70%;margin-bottom: 6px">' +
					data +
					'</span>'+

					'</div>' +
					'</div>' ;
			section.appendChild(div);

			//清空，重新接收
			currSpans = [];

			scrollWindow();

			var content = '';
			if(mode){
				content = "问题："+ question + ",答案：" + data + ";"
			}else{
				content = data
			}
			var msg = {
				"role" : "user",
				"content": content
			}
			var askRecord = {
				question : question,
				answer : data
			}
			askRecordList.push(askRecord);
			msglist.push(msg);


			if(currQ.length>0){
				deal();
			}else{
				//重新组装，及再问ds
				arkQ(an);
			}
		}

		function comit1(an){
			$("#bottom"+an).removeAttr("onclick");
			//获取输入文字
			var data = $("#input"+an).val();

			if(data == ""){
				alert("请输入...")
				return;
			}
			var section = document.getElementById("section");
			var div = document.createElement("div");
			div.innerHTML = '<div>' +
					'<img alt="" src="./images/example.jpg" class="img-radius" style="float: right" >' +
					'<div>' +
					'<span class="arrow-right" style="float: right;">&nbsp;</span>' +
					'<span class="user-span"  style="float: right">' +
					data +
					'</span>'+
					'</div>' +
					'</div>' ;
			section.appendChild(div);

			scrollWindow();

			var msg = {
				"role" : "user",
				"content":data
			}
			msglist.push(msg);


			if(currQ.length>0){

				deal();
			}else{
				//重新组装，及再问ds
				arkQ();

			}
		}

		function dealNot(){
			$("span").removeAttr("onclick");
			document.getElementById("input"+an1).onchange = "";

			var section = document.getElementById("section");
			var div = document.createElement("div");
			div.innerHTML = '<div>' +
					'<img alt="" src="./images/example.jpg" class="img-radius" style="float: right" >' +
					'<div>' +
					'<span class="arrow-right" style="float: right;">&nbsp;</span>' +
					'<span class="user-span"  style="float: right">' +
					'都不存在' +
					'</span>'+

					'</div>' +
					'</div>' ;
			section.appendChild(div);


			scrollWindow();

			if(currQ.length>0){
				deal();
			}else{
				//重新组装，及再问ds
				arkQ();

			}
		}

		function dealAnswer(data){
			$("span").removeAttr("onclick");
			document.getElementById("input"+an1).onchange = "";

			console.log(data);
			var section = document.getElementById("section");
			var div = document.createElement("div");
			div.innerHTML = '<div>' +
					'<img alt="" src="./images/example.jpg" class="img-radius" style="float: right" >' +
					'<div>' +
					'<span class="arrow-right" style="float: right;">&nbsp;</span>' +
					'<span class="user-span"  style="float: right">' +
					data +
					'</span>'+

					'</div>' +
					'</div>' ;
			section.appendChild(div);


			scrollWindow();

			var msg = {
				"role" : "user",
				"content":data
			}
			msglist.push(msg);


            if(currQ.length>0){

                deal();
            }else{
                //重新组装，及再问ds
                arkQ();

            }


		}

		function dealNotD(){
			$("span").removeAttr("onclick");
			document.getElementById("input"+an1).onchange = "";

			var section = document.getElementById("section");
			var div = document.createElement("div");
			div.innerHTML = '<div>' +
					'<img alt="" src="./images/example.jpg" class="img-radius" style="float: right" >' +
					'<div>' +
					'<span class="arrow-right" style="float: right;">&nbsp;</span>' +
					'<span class="user-span"  style="float: right">' +
					'暂无更多病症' +
					'</span>'+

					'</div>' +
					'</div>' ;
			section.appendChild(div);

			scrollWindow();

			//直接问答案
			ark();
		}

		function send(){

			if(!flag){
				alert("请选择以上问题选项...")
				return;
			}

			var input1 = $("#input0").val();

			if(input1==''){
				alert("请输入主诉...")
				return;
			}

			var input_0 = $("#input_0").val();
			if(input_0==''){
				alert("请输入持续时间...")
				return;
			}


			var input_1 = $("#input_1").val();
			if(input_1==''){
				input_1 = '无'
				return;
			}

			//$("#foot").hide();
			var ageText = '年龄：';
			var ageYear = $("#ageYear").val();
			if(ageYear > 150){
				alert("请输入正确岁数...")
				return;
			}else if(ageYear!=''){
				ageText += (ageYear + '岁');
			}
			var ageMonth = $("#ageMonth").val();
			if(ageMonth > 11){
				alert("请输入正确月数...")
				return;
			}else if(ageMonth!=''){
				ageText += (ageMonth + '月');
			}
			var ageDay = $("#ageDay").val();
			if(ageDay > 29){
				alert("请输入正确日数...")
				return;
			}else if(ageDay!=''){
				ageText += (ageDay + '日');
			}
			//var sexName = $("#sex").val();
			var sexName = currSexName;
			var yuejingshi = '';
			if(sexName == '女'){
				yuejingshi = $("#yuejingshi").val();
			}

			var yuejingshihtml = '';
			if(yuejingshi && yuejingshi!=''){
				yuejingshihtml = '最近一次月经时间'+yuejingshi +'，';
			}

			//当上一次还存在问题
			if(currQ.length>0){
				currW.push(input1);
				deal();
			}else{
				var askHtml = sexName + '，' + ageText + '，' + yuejingshihtml +'主诉：'+input1 + '，持续时间：'+ input_0 + '，过敏史：' + input_1;
				var section = document.getElementById("section");
				var div = document.createElement("div");
				div.innerHTML = '<div style="margin-top: 15px;">' +
						'<img alt="" src="./images/example.jpg" class="img-radius" style="float: right" >' +
						'<div>' +
						'<span class="arrow-right" style="float: right;">&nbsp;</span>' +
						'<span class="user-span"  style="float: right;max-width: 70%;margin-bottom: 15px">' +
						askHtml +
						'</span>'+

						'</div>' +
						'</div>' ;
				section.appendChild(div);

				scrollWindow();

				flag = false;

				//$("#input1").val('');

				if(input1 == '暂无更多病症'){
					ark();
				}else{
					//return;
					var msg = {
						"role" : "user",
						"content": "主诉：" + input1 + '，持续时间'+ input_0
					}
					zhushu = input1;
					msglist.push(msg);
					//放入就诊时间
					//放入姓名
					msglist.push({
						"role" : "user",
						"content":'就诊日期：' + new Date().format("yyyy-MM-dd hh:mm:ss")
					});
					//放入姓名
					msglist.push({
						"role" : "user",
						"content":'姓名：' + username
					});
					//放入年龄
					msglist.push({
						"role" : "user",
						"content":ageText
					});

					//放入性别
					msglist.push({
						"role" : "user",
						"content":sexName
					});

					//放入月经史
                    if(yuejingshi && yuejingshi!=''){
                        msglist.push({
                            "role" : "user",
                            "content":'最近一次月经时间' + yuejingshi
                        });
                    }

					//放入药物过敏史
					if(input_1 && input_1!=''){
						msglist.push({
							"role" : "user",
							"content":'药物过敏史：' + input_1
						});
					}

					var askRecord = {
						question : '主诉',
						answer : askHtml
					}
					askRecordList.push(askRecord);

					//清空点击事件

					$("#ageYear").attr("disabled", true);
					$("#ageMonth").attr("disabled", true);
					$("#ageDay").attr("disabled", true);
					$("#sex").attr("disabled", true);
					$("#yuejingshi").attr("disabled", true);
					$("#input0").attr("disabled", true);
					$("#input_0").attr("disabled", true);
					$("#input_1").attr("disabled", true);
					$("#send").hide();

					arkQ();
				}
			}

		}

		Date.prototype.format = function (format) {
			var o = {
				"M+": this.getMonth() + 1,
				"d+": this.getDate(),
				"h+": this.getHours(),
				"m+": this.getMinutes(),
				"s+": this.getSeconds(),
				"q+": Math.floor((this.getMonth() + 3) / 3),
				"S": this.getMilliseconds()
			}
			if (/(y+)/.test(format)) {
				format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
			}
			for (var k in o) {
				if (new RegExp("(" + k + ")").test(format)) {
					format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
				}
			}
			return format;
		}

		//设置滚动条到最底部
		function scrollWindow(){
			scroll(0, 100000);
		}

	</script>
</head>
<body ontouchstart>

<section>
	<div id="section"></div>
</section>
<div class="weui_dialog_confirm" id="warningBox" style="display: none;">
	<div class="weui_mask"></div>
	<div class="weui_dialog" style="border-radius: 5px !important;">
		<div style="position: relative;">
			<img alt="" src="./images/docter.jpg" style="width: 55px;height: 55px;border-radius: 50%;margin: 5px;" >
			<!-- <img alt="" src="" style="width: 100%;"> -->
			<p id="warningBoxText" style="padding: 5px 5px 0px 5px;"></p>
			<span class="del fa fa-times" aria-hidden="true" onclick="OK()" style="position: absolute; top: 2px;right: 2px;font-size: 24px;"></span>
		</div>
	</div>
</div>
<div class="weui_dialog_confirm" id="picBox" style="display: none;">
	<div class="weui_mask"></div>
	<div class="weui_dialog" >
		<div style="margin:15px 0;border-bottom: 1px solid #efefef;padding-bottom:8px;">症状图片</div>
		<div id="im" style="height:300px; overflow:auto;"></div>
		<div class="weui_dialog_ft">
			<a href="javascript:;" class="weui_btn_dialog primary" onclick="IMG_OK()">确定</a>
		</div>
	</div>
</div>
<div id="loadingToast" class="weui_loading_toast" style="display: none;">
	<div class="weui_mask_transparent"></div>
	<div class="weui_toast">
		<div class="weui_loading">
			<div class="weui_loading_leaf weui_loading_leaf_0"></div>
			<div class="weui_loading_leaf weui_loading_leaf_1"></div>
			<div class="weui_loading_leaf weui_loading_leaf_2"></div>
			<div class="weui_loading_leaf weui_loading_leaf_3"></div>
			<div class="weui_loading_leaf weui_loading_leaf_4"></div>
			<div class="weui_loading_leaf weui_loading_leaf_5"></div>
			<div class="weui_loading_leaf weui_loading_leaf_6"></div>
			<div class="weui_loading_leaf weui_loading_leaf_7"></div>
			<div class="weui_loading_leaf weui_loading_leaf_8"></div>
			<div class="weui_loading_leaf weui_loading_leaf_9"></div>
			<div class="weui_loading_leaf weui_loading_leaf_10"></div>
			<div class="weui_loading_leaf weui_loading_leaf_11"></div>
		</div>
		<p class="weui_toast_content">正在分析...</p>
	</div>
</div>
<div class="weui_dialog_confirm" id="disclaimerBox" style="display: none;">
	<div class="weui_mask"></div>
	<div class="weui_dialog" >
		<div style="margin:15px 0;border-bottom: 1px solid #efefef;padding-bottom:8px;font-size: 20px;">郑重承诺</div>
		<p style="color: #000;font-size: 13px;">我已经阅读并遵守<a>《<!-- 蒙估大夫 -->免责声明》</a></p>
		<div class="weui_dialog_ft">
			<a href="javascript:;" class="weui_btn_dialog primary" onclick="Disclaimer_OK()">确定</a>
			<a href="javascript:;" class="weui_btn_dialog primary" style="color: #ff0000 !important;" onclick="Disclaimer_ON()">取消</a>
		</div>
	</div>
</div>
<div style="height: 1px">
</div>
<footer style="bottom: 10px">
	<ul>
		<%--<div id="foot">
			<input class="input" id="input1" placeholder="   请输入..." style="text-indent: 5px;width: 70%;height: 40px;border-radius: 8px;margin-left: 8px"/>
			<bottom onclick="send();" id="selector-1" style="background-color:#488378 !important;margin: 8px 0px;padding: 10px 15px;color: #fff;border-radius: 5px;">发送</bottom>
		</div>--%>
	</ul>
	<bottom id="try1" onclick="try1()" style="padding: 8px 15px;border-radius: 5px;background-color: #ffd801;color: #fff;display: flex;text-align: center;justify-content: center;">重试</bottom>
</footer>
</body>
</html>
